package com.lkm.market2_0.m_imple.steelyarddata;

import android.os.Looper;

import com.google.gson.annotations.SerializedName;
import com.lkm.frame.entity.ResponEntityAbs;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.market2_0.AppDataCache;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.OrganizationInfo;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.netapi.AppApi;
import com.lkm.market2_0.task.DefaultTaskExecutor;
import com.lkm.market2_0java.help.GsonHelp;

import java.util.Map;



class UpSteelyardRegTask extends TaskAdapter<SteelyardInfo, Void, ResponEntityAbs<Void>> {


    public UpSteelyardRegTask() {
        super(new DefaultTaskExecutor(Looper.getMainLooper(), true), UpSteelyardRegTask.class.getName(), ExpandConfig.instance.getTaskManager2());
    }

    @Override
    public void doExecuting(SteelyardInfo param) {
        super.doExecuting(param);

        mTaskExecutor.executor(new Runnable() {
            @Override
            public void run() {
                ayncdoExecuting();
            }
        });

    }

    private void ayncdoExecuting() {

        SteelyardRegTO steelyardRegTO = new SteelyardRegTO();

        SteelyardInfo steelyardInfo = getParam();

        OrganizationInfo organizationInfo = AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg());

        steelyardRegTO.balance_no = steelyardInfo.getSteelyardMac();
        if(organizationInfo!=null) {
            steelyardRegTO.mark_address = organizationInfo.getAddress();
            steelyardRegTO.mark_phone = organizationInfo.getContactPhone();
            steelyardRegTO.mark_name = organizationInfo.getName();
            steelyardRegTO.mark_user = organizationInfo.getContactMan();
        }

        steelyardRegTO.stall_license = steelyardInfo.getStallEntity().businessentity;
        steelyardRegTO.stall_license_no = steelyardInfo.getStallEntity().creditcode;
        steelyardRegTO.stall_name = steelyardInfo.getStallEntity().getStallName();
        steelyardRegTO.stall_no = steelyardInfo.getStallEntity().getStallNo();
        steelyardRegTO.stall_phone = steelyardInfo.getStallEntity().getPhone();
        steelyardRegTO.stall_type = steelyardInfo.getStallEntity().type;
        steelyardRegTO.stall_user = steelyardInfo.getStallEntity().getName();
        steelyardRegTO.exit_factory_id = steelyardInfo.b_id;


        Map<String, Object> stringStringMap = GsonHelp.fromJson(GsonHelp.toJson(steelyardRegTO),new com.google.gson.reflect.TypeToken<Map<String, String>>() {}.getType());

        String orgId = ""+ steelyardInfo.getOrg();
        String ukey = steelyardInfo.getSteelyardMac();

        String json = AppApi.upSteelyardRegTask(orgId,ukey,stringStringMap,this);

        ResponEntity<Void> responEntity = ResponEntity.fromJson(json, null, "");




        if (responEntity.isSuccess()) {
            complete(new ResponEntity<Void>().success(null, responEntity.getMsg()));

        } else {

            complete(new ResponEntity<Void>().fail(null, responEntity.getMsg()));
        }


    }



    private static class SteelyardRegTO {

        @SerializedName("balance_no")
        public String balance_no;

        @SerializedName("stall_no")
        public String stall_no;

        @SerializedName("stall_name")
        public String stall_name;

        @SerializedName("stall_license")
        public String stall_license;

        @SerializedName("stall_license_no")
        public String stall_license_no;

        @SerializedName("stall_type")
        public String stall_type;

        @SerializedName("stall_user")
        public String stall_user;

        @SerializedName("stall_phone")
        public String stall_phone;

        @SerializedName("mark_user")
        public String mark_user;

        @SerializedName("mark_name")
        public String mark_name;

        @SerializedName("mark_address")
        public String mark_address;

        @SerializedName("mark_phone")
        public String mark_phone;

        @SerializedName("mark_license")
        public String mark_license;

        @SerializedName("mark_license_no")
        public String mark_license_no;

        @SerializedName("exit_factory_id")
        public String exit_factory_id;
    }
}
